package reflect;


import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @description: 通过反射获取类其他的信息
 * @author: apple
 * @date:  2022/1/9 11:04 下午
 * @version 1.0
 **/
public class OtherTest {

    @Test
    public void test1() {

        final Class<Person> personClass = Person.class;

        // 获取运行时父类
        final Class<? super Person> superclass = personClass.getSuperclass();
        System.out.println(superclass);

        // 获取运行时泛型父类
        final Type genericSuperclass = personClass.getGenericSuperclass();
        System.out.println(genericSuperclass);

        // 获取运行时父类的泛型类型
        ParameterizedType t = (ParameterizedType)genericSuperclass;
        final Type[] actualTypeArguments = t.getActualTypeArguments();
        System.out.println(actualTypeArguments.length);
        // 对象的类型
        System.out.println(actualTypeArguments[0].getClass());
        System.out.println(actualTypeArguments[0].getClass() == (Class)actualTypeArguments[0]);

        // 把对象转换为子类
        Class ct = (Class)actualTypeArguments[0];
        System.out.println(ct.getName());


    }


    @Test
    public void test2() {
        final Class<Person> personClass = Person.class;
        // 获取实现的接口
        final Class<?>[] interfaces = personClass.getInterfaces();
        for (Class<?> t : interfaces) {
            System.out.println(t);
        }


        // 获取运行时包
        final Package aPackage = personClass.getPackage();
        System.out.println(aPackage);


        // 获取运行时类的注解
        final Annotation[] annotations = personClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

    }
}
